package com.zxy.paycenter.common.config;

import com.xiaoleilu.hutool.crypto.SecureUtil;
import com.zxy.paycenter.common.utils.DateUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger 配置类
 *
 * @author 周徐阳
 * @date 2019-07-23
 */
@Configuration
@EnableSwagger2
@ConditionalOnExpression("${swagger.show}")
public class SwaggerConfig {

    private static synchronized List<Parameter> getPars() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").defaultValue("TOKEN_PAY_b06045fc1307e818703f303c50831537").required(true).build();
        ParameterBuilder requestIdPar = new ParameterBuilder();
        String requestId = String.valueOf(System.currentTimeMillis());
        requestIdPar.name("requestId").description("流水号").modelRef(new ModelRef("string")).parameterType("header").defaultValue(requestId).required(true).build();
        ParameterBuilder channelNoPar = new ParameterBuilder();
        String channelNo = "XN";
        channelNoPar.name("channelNo").description("渠道号(XN=西宁,BT=包头)").modelRef(new ModelRef("string")).parameterType("header").defaultValue(channelNo).required(true).build();
        ParameterBuilder timeStampPar = new ParameterBuilder();
        String timeStamp = String.valueOf(DateUtils.getNowTimeStamp());
        timeStampPar.name("timeStamp").description("时间戳").modelRef(new ModelRef("string")).parameterType("header").defaultValue(timeStamp).required(true).build();
        ParameterBuilder versionPar = new ParameterBuilder();
        String version = "v1";
        versionPar.name("version").description("版本号").modelRef(new ModelRef("string")).parameterType("header").defaultValue(version).required(true).build();
        ParameterBuilder hmacPar = new ParameterBuilder();
        hmacPar.name("hmac").description("加密码").modelRef(new ModelRef("string")).parameterType("header").defaultValue(SecureUtil.md5("requestId=" + requestId + "&channelNo=" + channelNo + "&timeStamp=" + timeStamp + "&version=" + version + "we73hg3").toUpperCase()).required(true).build();
        pars.add(tokenPar.build());
        pars.add(requestIdPar.build());
        pars.add(channelNoPar.build());
        pars.add(timeStampPar.build());
        pars.add(hmacPar.build());
        pars.add(versionPar.build());
        return pars;
    }

    @Bean
    public Docket webApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("支付后台API接口文档")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxy.paycenter.modules.page"))
                .paths(PathSelectors.any()).build()
                .globalOperationParameters(getPars());
    }

    @Bean
    public Docket alipayApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("支付宝API接口文档")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxy.paycenter.modules.alipay"))
                .paths(PathSelectors.any()).build()
                .globalOperationParameters(getPars());
    }

    @Bean
    public Docket weixinpayApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("微信API接口文档")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxy.paycenter.modules.wechat"))
                .paths(PathSelectors.any()).build()
                .globalOperationParameters(getPars());
    }

    @Bean
    public Docket unionpayApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("银联API接口文档")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxy.paycenter.modules.unionpay"))
                .paths(PathSelectors.any()).build()
                .globalOperationParameters(getPars());
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return api文档信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("支付中心系统")
                //创建人
                .contact(new Contact("周徐阳", "https://blog.csdn.net/tavatimsa", "tavatimsa@163.com"))
                //版本号
                .version("1.0")
                //描述
                .description("集成支付宝、微信和银联的支付服务")
                .build();
    }
}
